home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 920 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  66 lines

  1. Path: rover.ucs.ualberta.ca!news
  2. From: ryangall@gpu.srv.ualberta.ca (Bobby Sixkiller)
  3. Newsgroups: comp.lang.c
  4. Subject: allocating unlimited string input....HELP
  5. Date: 10 Jan 1996 07:23:18 GMT
  6. Organization: University of Alberta, Edmonton, Canada
  7. Message-ID: <4cvph6$s8s@pulp.ucs.ualberta.ca>
  8. NNTP-Posting-Host: async4-15.remote.ualberta.ca
  9. Mime-Version: 1.0
  10. X-Newsreader: WinVN 0.99.2
  11.  
  12. I am doing an assignment for my computing class, and seem to have run into 
  13. a small problem. I am supposed to be able to read an unlimited string from 
  14. stdin, without defining maximum size. I thought that this problem would be 
  15. simple....but I have been at it for a while now. Ill show you what my last 
  16. resort was.....
  17.  
  18.  
  19.  
  20. int read(char *S)
  21. {
  22.  char *A,*B;
  23.  char ch,count=0;
  24.  
  25.   A=NULL;
  26.   B=NULL;
  27.  
  28.   ch=getc(stdin);
  29.  
  30.   while(ch!='\n')
  31.   {
  32.     B=(char *) malloc(count+2);
  33.     if(count>0)
  34.     { 
  35.       sprintf(B,"%s%c",A,ch);
  36.       free(A);
  37.     }
  38.     else sprintf(B,"%c",ch);
  39.  
  40.     printf("%s---%i----%i\n",B,strlen(B),count);
  41.     A=B;
  42.     ch=getc(stdin);
  43.     count=count+1;
  44.   }
  45.  
  46. }
  47.  
  48.  
  49. I know its ugly, but its the third version....and I tried all the other 
  50. possibilities that I know of that would have better style. Anyways,  they 
  51. all pretty much are doing the same thing...oh yeah, about char *S, thats 
  52. for later, Im still stuck trying to get the bulk of the function to work, 
  53. once its working, I will assign A to S........
  54.  
  55. Anyways, the function reads in just fine, but for some reason when it gets 
  56. to 146 characters it bails out, loses char *B and starts reducing 
  57. count?!&*#$@   what the hell?! I know I can uses some data structure, or 
  58. just a linked list to solve this, but this assignment limits you to one 
  59. module, and no header files....and I dont really want to start haveing 
  60. 2000 lines of code for such a simple problem?!  I guess they think its 
  61. simple enough.....so did I?!
  62.  
  63.  
  64. please mail me ASAP....thanks!
  65.  
  66.